package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TCRoleAcctInfoMapper;
import cc.rengu.igas.momp.common.dao.TMMcFundInfoMapper;
import cc.rengu.igas.momp.common.dao.impl.TCRoleAcctInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.TMMcFundInfoMapperImpl;
import cc.rengu.igas.momp.common.entity.TCRoleAcctInfo;
import cc.rengu.igas.momp.common.entity.TMMcFundInfo;
import cc.rengu.igas.momp.common.enums.QueryTypeEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.request.MchntMcAccountRechargeRequest;
import cc.rengu.igas.momp.facade.response.MchntMcAccountRechargeResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 商户营销活动资金打款成功通知接口dubbo实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/7/8 15:23
 */

public class MchntMcAccountRechargeNotificationService extends RadpService {

    /**
     * 法人机构号
     */
    private String globalInstId;
    /**
     * 操作人
     */
    private String globalQueryId;
    /**
     * 当前系统日期
     */
    private String globalTimeStamp;

    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            MchntMcAccountRechargeRequest mchntMcAccountRechargeRequest = new MchntMcAccountRechargeRequest();
            ConvertUtil.convertOutput(mchntMcAccountRechargeRequest);

            MchntMcAccountRechargeResponse mchntMcAccountRechargeResponse = new MchntMcAccountRechargeResponse();
            mchntMcAccountRechargeResponse.setHeader(mchntMcAccountRechargeRequest.getHeader());
            BizResponse<MchntMcAccountRechargeResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(mchntMcAccountRechargeResponse);

            /* 检查报文 */
            messageValidation(mchntMcAccountRechargeRequest);

            /* 服务调用 */
            BizResponse<MchntMcAccountRechargeResponse> bizResponseNew = editMoneyOffMcDetailInfo(mchntMcAccountRechargeRequest, mchntMcAccountRechargeResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }

    /**
     * 商户营销活动资金打款成功通知业务实现
     *
     * @param mchntMcAccountRechargeRequest  商户营销活动资金打款接口请求对象
     * @param mchntMcAccountRechargeResponse 商户营销活动资金打款接口应答对象
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.MchntMcAccountRechargeResponse>
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/7/8 15:38
     */
    private BizResponse<MchntMcAccountRechargeResponse> editMoneyOffMcDetailInfo(MchntMcAccountRechargeRequest mchntMcAccountRechargeRequest, MchntMcAccountRechargeResponse mchntMcAccountRechargeResponse) throws Exception {

        BizResponse<MchntMcAccountRechargeResponse> bizResponse = new BizResponse<>();

        /* 查询当前数据库中是否有记录,如果有的话,取最大的记录顺序号,没有的话记录顺序号为-1 */
        int recordIndex = checkRecordIndex(mchntMcAccountRechargeRequest);
        recordIndex++;

        insertSingleRecordForMchntFundInfo(recordIndex, mchntMcAccountRechargeRequest);

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        mchntMcAccountRechargeResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        mchntMcAccountRechargeResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        bizResponse.setResult(mchntMcAccountRechargeResponse);

        return bizResponse;
    }

    /**
     * 请求报文体内容检查
     *
     * @param mchntMcAccountRechargeRequest 商户营销活动资金打款成功通知接口请求对象
     * @throws BizException 异常信息
     * @author Jinan Liu
     * @date 2020/7/8 15:50
     */
    private void messageValidation(MchntMcAccountRechargeRequest mchntMcAccountRechargeRequest) throws BizException {

        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getHeader().getInstId())) {
            rglog.error("法人机构号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = mchntMcAccountRechargeRequest.getHeader().getInstId();
        }

        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getHeader().getTraceNo())) {
            rglog.error("流水号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getQueryType())) {
            rglog.error("操作员类型为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getQueryId())) {
            rglog.error("操作员ID为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryId = mchntMcAccountRechargeRequest.getQueryId();
        }

        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getQueryRole())) {
            rglog.error("操作员角色为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getQueryOrgId())) {
            rglog.error("操作员所属法人机构为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());

        /* 营销活动编号 */
        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getMcNo())) {
            rglog.error("营销活动编号为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 打款金额 */
        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getTxnAmt())) {
            rglog.error("打款金额为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 账户信息-付款账号 */
        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getAcctInfoObj().getPayAcctNo())) {
            rglog.error("账户信息-付款账号为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 账户信息-付款账号名称 */
        if (StringUtil.isNullorEmpty(mchntMcAccountRechargeRequest.getAcctInfoObj().getPayAcctName())) {
            rglog.error("账户信息-付款账号名称为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
    }

    /**
     * 查询当前最大记录顺序号
     *
     * @param mchntMcAccountRechargeRequest 商户营销活动资金打款成功通知接口请求对象
     * @return int 当前最大记录顺序号
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/7/8 17:33
     */
    private int checkRecordIndex(MchntMcAccountRechargeRequest mchntMcAccountRechargeRequest) throws Exception {

        /* 组装数据库操作实体类 */
        TMMcFundInfo tmMcFundInfo = new TMMcFundInfo();
        tmMcFundInfo.setInstId(globalInstId);
        tmMcFundInfo.setMcNo(mchntMcAccountRechargeRequest.getMcNo());
        tmMcFundInfo.setPayerAcctId(globalQueryId);
        tmMcFundInfo.setPayerAcctType(QueryTypeEnum.MCHNT_QYERY_TYPE.getQueryTypeCode());

        /* 查询数据 */
        TMMcFundInfoMapper tmMcFundInfoMapper = new TMMcFundInfoMapperImpl();
        int recordIndex = tmMcFundInfoMapper.checkMaxmiumRecordIndexInfo(tmMcFundInfo);
        if (CommonConstant.ZERO <= recordIndex) {
            /* 数据库有记录,直接返回当前数据 */
            return recordIndex;
        } else {
            /* 数据库无记录,返回-1 */
            return CommonConstant.MINUS_ONE;
        }
    }

    /**
     * 将数据插入数据库
     *
     * @param recordIndex                   记录顺序号
     * @param mchntMcAccountRechargeRequest 商户营销活动资金打款成功通知接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/7/8 17:09
     */
    private void insertSingleRecordForMchntFundInfo(int recordIndex, MchntMcAccountRechargeRequest mchntMcAccountRechargeRequest) throws Exception {

        /* 取数据库收款账户信息数据 */
        String headOfficeId = globalInstId + CommonConstant.DEFAULT_HEAD_OFFICE_SUFFIX;
        TCRoleAcctInfoMapper tcRoleAcctInfoMapper = new TCRoleAcctInfoMapperImpl();
        TCRoleAcctInfo tcRoleAcctInfo = tcRoleAcctInfoMapper.selectInstitutionMcAccountByOrgIdAndAccountCategory(globalInstId, CommonConstant.MOMP_SUBJECT, headOfficeId);
        if (null == tcRoleAcctInfo) {
            rglog.error("获取法人机构<{}>的总行机构<{}>的营销收款账户信息失败!", globalInstId, headOfficeId);
            throw new BizException(RespCodeEnum.NO_MC_ACCOUNT_INFO_ERROR.getRespCode(), RespCodeEnum.NO_MC_ACCOUNT_INFO_ERROR.getRespDesc());
        }

        /* 组装数据库操作实体类 */
        TMMcFundInfo tmMcFundInfo = new TMMcFundInfo();
        tmMcFundInfo.setInstId(globalInstId);
        tmMcFundInfo.setMcNo(mchntMcAccountRechargeRequest.getMcNo());
        tmMcFundInfo.setRecordIndex(Integer.toString(recordIndex));
        tmMcFundInfo.setPayerAcctId(globalQueryId);
        tmMcFundInfo.setPayerAcctType(QueryTypeEnum.MCHNT_QYERY_TYPE.getQueryTypeCode());
        tmMcFundInfo.setPayerAcctNo(mchntMcAccountRechargeRequest.getAcctInfoObj().getPayAcctNo());
        tmMcFundInfo.setPayerAcctName(mchntMcAccountRechargeRequest.getAcctInfoObj().getPayAcctName());
        tmMcFundInfo.setPayeeAcctNo(tcRoleAcctInfo.getActualAcct());
        tmMcFundInfo.setFundAmt(mchntMcAccountRechargeRequest.getTxnAmt());
        tmMcFundInfo.setOprId(CommonConstant.DEFAULT_OPR_ID);
        tmMcFundInfo.setCreateTime(globalTimeStamp);

        /* 插入数据 */
        TMMcFundInfoMapper tmMcFundInfoMapper = new TMMcFundInfoMapperImpl();
        int returnCode = tmMcFundInfoMapper.insertSingleRecordForMchntFundInfo(tmMcFundInfo);
        if (CommonConstant.PROCESS_SUCCESS != returnCode) {
            rglog.error("法人机构<{}>的营销活动<{}>的出资方<{}>的打款通知数据新增失败! RETURN_CODE=<{}>", globalInstId, mchntMcAccountRechargeRequest.getMcNo(), globalQueryId, returnCode);
            throw new BizException(RespCodeEnum.RECORD_MC_FUND_INFO_ERROR.getRespCode(), RespCodeEnum.RECORD_MC_FUND_INFO_ERROR.getRespDesc());
        }
    }
}
